<?php
class YouNet_Util 
{  
	
	/**
	 * Enter description here ...
	 * @param string $tableName
	 * @param string $moduleName
	 * @return YouNet_Db_Table
	 */
	public static function getDbTable($tableName, $moduleName = 'Core') 
	{
		$className = ucfirst($moduleName) . '_Model_DbTable_' . ucfirst($tableName);
		return new $className();
	}
	
	public static function getDbRow($tableName, $moduleName = 'Core')
	{
		$dbTable = self::getDbTable($tableName, $moduleName);
		return $dbTable->createRow();
	}
	
	public static function redirect($url) 
	{
		header("Location: $url");
		exit(0);
	}
	
	public static function parentRedirect($url) 
	{
		echo "<script language='javascript'>top.location.href='" . $url . "'</script>";
		exit(0);
	}
	
	public static function translate($text) 
	{
		$translator = Zend_Registry::get('Zend_Translate');
		return $translator->translate($text);
	}
	
	public static function escapeForJs($text)
	{
		//TODO look urgly :|
		$value = str_replace("\r\n", '\n', htmlentities($text, ENT_QUOTES, 'utf-8'));
		return str_replace("\n", '\n', $value);
	}
	
	public static function buildUrlFromArray($arr)
	{
		foreach ($arr as $name => $value)
		{
			return urlencode(implode('&', $name . '=' . $value));
		}
	}
	
	public static function assignPageValues(&$view, $page_count, $current_page, $query = array())
	{
		$view->pageCount = $page_count;
        $view->query = $query;
        $view->current = $current_page;
        $view->page = $current_page;
        if ($current_page > 1) $view->previous = $current_page - 1;
        if ($current_page < $page_count) $view->next = $current_page + 1;
        $pagesInRange = array();
        for ($i=1; $i<=$page_count; $i++) 
        	$pagesInRange[] = $i;
        $view->pagesInRange = $pagesInRange;
	}
	
	/**
	 * @deprecated
	 * @return Zend_Cache_Backend
	 */
	public static function getBackendCache()
	{
		$cacheManager = Zend_Registry::get('cache_manager');
		return $cacheManager->getObjectMemcached();
	}
	
	/**
	 * @deprecated
	 * @return Zend_Cache_Core
	 */
	public static function getFrontendCache()
	{
		$cacheManager = Zend_Registry::get('cache_manager');
		return $cacheManager->getObjectMemcached();
	}

	/**
	 * @deprecated
	 * @return Zend_Cache_Core
	 */
	public static function getMemCache()
	{
		$cacheManager = Zend_Registry::get('cache_manager');
		return $cacheManager->getObjectMemcached();
	}
	
	/**
	 * @return Zend_Cache_Core
	 */
	public static function getObjectMemCache()
	{
		$cacheManager = Zend_Registry::get('cache_manager');
		return $cacheManager->getObjectMemcached();
	}
	
	/**
	 * @return Zend_Cache_Core
	 */
	public static function getObjectFileCache()
	{
		$cacheManager = Zend_Registry::get('cache_manager');
		return $cacheManager->getObjectFileCache();
	}
	
	/**
	 * @return string
	 */
	public static function createFullUrl($url, $prefix = 'http')
	{
		if (substr($url, 0, 2) == '//')
		{
			return $prefix . ':' . $url;
		}
		
		return $url;
	}
    
    /**
     *
     * @return fanpage install url 
     */
    public static function getFanpageInstallUrl()
    {
        $social = YouNet_Api::getInstance()->getSocialPlugin();
        
        $footer_url = "/?install_page=1";
        $session = Zend_Registry::get('session');
        if (empty($session->ref))
        {
            $footer_url = $footer_url . '&ref=page_' . $social->getIdPage();
        }
        return $social->url($footer_url);
    }
	
	/**
	 * Preprocess html text to string text used by JavaScript
	 * @param string $input
	 * @return string
	 */
	public static function htmlToJs($input)
	{
		$order = array("\r\n", "\n", "\r");
		$output =  str_replace($order, '', $input);
		$output =  str_replace("'", "\'", $output);
		return $output;
	}
}
